﻿var full = false;

var cookieList = function (cookieName) {
    var cookie = $.cookie(cookieName);
    var total = 0;
    var item = cookie ? cookie.split(',') : new Array();
    var items = new Array();
    for (var x = 0; x < Math.floor(item.length / 6); x++) {
        items[x] = new Array();
    }

    var i = 0;
    for (var x = 0; x < item.length; x++) {
        var index = Math.floor(x / 6);
        items[index][i] = item[x];
        if (i == 3 || i == 4 || i == 5) {
            items[index][i] = parseFloat(items[index][i]);
        }
        i++;
        if (i == 6) i = 0;
    }

    return {
        "add": function (val) {
            var index = this.find(val[0], val[1]);
            if (index != -1) { //found
                // items[index][3] = parseFloat(items[index][3]);
                //items[index][3] += val[3];
                //$.cookie(cookieName, items, { path: '/' });
                this.del(val[0], val[1]);
                
            }
            //else {

            items.push(val);
            $.cookie(cookieName, items, { path: '/' });
            n += 1;
            //}
            this.updatePrice();
            getCart(false);
        },
        "clear": function () {
            $.cookie(cookieName, null, { path: '/' });
            this.updatePrice();
        },
        "items": function () {
            return items;
        },
        "del": function (id, type) {
            var index = this.find(id, type);
            //items[index][3] = parseFloat(items[index][3]);

            if (index != -1) { //found
                /*if (items[index][3] <= 1) { // check quality
                items.splice(index, 1);
                $.cookie(cookieName, items, { path: '/' });
                n -= 1;
                }
                else {
                */
                items.splice(index, 1);
                $.cookie(cookieName, items, { path: '/' });
                n -= 1;
                // }
            }
            this.updatePrice();

        },

        "find": function (id, type) {
            for (var x = 0; x < items.length; x++) {
                if ((items[x][0] == id) && (items[x][1] == type)) {
                    this.updatePrice();
                    return x;
                }
            }
            this.updatePrice();
            return -1;
        },
        "updatePrice": function () {
            total = 0;

            for (var x = 0; x < items.length; x++) {
                items[x][5] = items[x][3] * items[x][4];
                total += items[x][5];
            }
        },
        "totalPrice": function () {
            return total;
        }
    }
}

var list = new cookieList("Cart");
var n = list.items().length;
list.updatePrice();

function getCart(type) {
    if (type == false && list.totalPrice() > 0) {
        document.getElementById('cart-item').innerHTML = "";
        for (var x = 0; x < n; x++) {
            document.getElementById('cart-item').innerHTML += "<span id='item_" + x + "'></span>";
            
            document.getElementById('item_' + x).innerHTML += "<div style='width:230px;float:left;'>" + list.items()[x][2] + "</div>";  //Name
            document.getElementById('item_' + x).innerHTML += "<div style='width:25px;float:left;'>" + list.items()[x][3] + "</div>";   //Quality
            document.getElementById('item_' + x).innerHTML += "<div style='width:20px;float:left;'>x</div>";   //x
            document.getElementById('item_' + x).innerHTML += "<div style='width:40px;float:left;'>$" + list.items()[x][4] + "</div>";   //Unit Price
            document.getElementById('item_' + x).innerHTML += "<div style='width:20px;float:left;'>=</div>";   //x
            document.getElementById('item_' + x).innerHTML += "<div style='width:45px;float:left;font-weight: bold;text-align: right;'>$" + list.items()[x][5] + "</div>";   //Total

            document.getElementById('item_' + x).innerHTML += "<div style='width:50px;float:left;margin: -5px 0 0 15px;'><input type='button' id='clear_" + x + "' value='Del' onclick='javascript:clearitem(" + x + ")'/></div>";
        }
        document.getElementById('cart-item').innerHTML += "<div style='float: left;margin: 0 20px 0 100px;'>Total : $" + list.totalPrice() + "</div>";
        document.getElementById('cart-item').innerHTML += "<div style='width:50px;float:left;margin: -5px 0 0 15px;'><a href='/Account/CheckOut'><input type='button' id='clear_" + x + "' value='Thanh Toán'/></a></div>";
        //document.getElementById('cart-item').innerHTML += '<form method="post" action="https://www.nganluong.vn/advance_payment.php"><input type=hidden name=receiver value="vinhphuoc91@gmail.com" /><input type=hidden name=product value="<!--Mã đơn đặt hàng-->" /><input type=hidden name=price value="' + list.totalPrice() + '" /><input type=hidden name=return_url value="<!--URL thanh toán thành công-->" /><input type=hidden name=comments value="<!--Ghi chú về đơn hàng-->" /><input type=image src="https://www.nganluong.vn/data/images/buttons/7.gif" /></form>';
        full = true;
    }
    else if (type == true) {
        document.getElementById('cart-item').innerHTML = "";
        document.getElementById('cart-item').innerHTML = "There are " + n + " products in your cart";
        document.getElementById('cart-item').innerHTML += "<br><br><span style=\"text-align:right;\">Total: " + list.totalPrice() + "</span>";
        full = false;
    }
}
$("#cart-item").click(function () {
    if (full == false) {
        getCart(false);
    } else {
        getCart(true);
    }

});


function clearitem(x) {
    list.del(list.items()[x][0], list.items()[x][1]);
    getCart(true);
}

getCart(true);

